﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.XWLW;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;

public partial class AddPM : System.Web.UI.Page
{
    /// <summary>
    /// 学位论文操作对象
    /// </summary>
    private XWLW_DAO xwlwDAO = new XWLW_DAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {
        
        String message = null;
        //获取分类编码
        String flbm = Request["flbm"];
        //年份
        String nf = Request["nf"];
        //批次(期号)
        String qh = Request["qh"];
        XWLW_FL xwlwFL = null;
        //验证年份
        if (null == nf || nf.Length != 4)
        {
            message = "错误：年份信息有误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        //验证批次(期号)
        if (null == qh || qh.Length != 2)
        {
            message = "错误：期号信息有误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        if (null == flbm || "".Equals(flbm))
        {
            message = "错误：分类编码为空";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        else
        {
            xwlwFL = xwlwDAO.GetXWLW_FLByFlbm(flbm);
            if (null == xwlwFL)
            {
                message = "错误：没有找到对应的分类编码";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return;
            }
        }

        //设置返回按钮链接
        this.backHyperLink.NavigateUrl = GlobalXWLW.LISTPM_PAGE + "?catalog=" + flbm;

        XWLW_PM xwlwPM = this.xwlwDAO.getMaxXWLW_PM(flbm, nf,qh);
       
        
        //分类编码
        this.flbmTextBox.Text = xwlwFL.flbm;
        //内容
        this.nrTextBox.Text = xwlwFL.nr;
        //索引编码
        this.sybmTextBox.Text=flbm+'-'+nf+'-'+qh;
        //年份
        this.nfTextBox.Text = nf;

        //中文题名
        this.zwtmTextBox.Text = "";
       
        //外文题名
        this.ywtmTextBox.Text ="";
        
        //作者
        this.zzTextBox.Text = "";
        //导师
        this.dsTextBox.Text = "";
        //论文级别
        this.lwjbTextBox.Text = "";
        //学科专业名称
        this.xkzymcTextBox.Text = "";
        //分类号
        this.flhTextBox.Text = "";
        //学位授予单位
        this.xwsydwTextBox.Text = "";
        //论文提交日期
        this.lwtjrqTextBox.Text = "";
        //论文答辩日期
        this.lwdbrqTextBox.Text = "";
        //学位授予日期
        this.xwsyrqTextBox.Text = "";
        //中文主题词
        this.zwztcTextBox.Text = "";
        //中文摘要
        this.zwzyTextBox.Text = "";
        //英文主题词
        this.ywztcTextBox.Text = "";
        //英文摘要
        this.ywzyTextBox.Text = "";
        //分类体系
        this.fltxTextBox.Text = "";
        //原文件名
        //出版日期
        this.cbrqTextBox.Text = "";
        //页数
        this.ysHiddenField.Value = "";
        //获取文件名信息,不包括后缀
        String wjm = null;
        if (null == xwlwPM)
        {
            wjm = GetWJM(null);
        }
        else
        {
            wjm = GetWJM(xwlwPM.wjm);
        }
        //文件名
        this.wjmTextBox.Text = wjm ;

        User user = (User)Session["login"];
        if (null == user)
        {
            GlobalMethod.RedirectLogin(Response);
        }
        else
        {
            this.dlrTextBox.Text = user.name;
        }
    }

    /// <summary>
    /// 获取用户填写的学位论文篇名信息
    /// </summary>
    /// <returns></returns>
    private XWLW_PM GetXWLW_PM()
    {
        XWLW_PM xwlwPM = new XWLW_PM();
        //分类编码
        xwlwPM.flbm = this.flbmTextBox.Text;
        //索引编码
        xwlwPM.sybm = this.sybmTextBox.Text;
        
        //文件名
        xwlwPM.wjm = this.wjmTextBox.Text;
        //原文件名
        xwlwPM.ywjm = this.ywjmFileUpload.FileName;
        //中文题名
        xwlwPM.zwtm = this.zwtmTextBox.Text.Trim();
        //英文题名
        xwlwPM.ywjm = this.ywtmTextBox.Text.Trim();
        //作者
        xwlwPM.zz = this.zzTextBox.Text.Trim();
        //导师
        xwlwPM.ds = this.dsTextBox.Text.Trim();
        //论文级别
        xwlwPM.lwjb = this.lwjbTextBox.Text.Trim();
        //学科专业名称
        xwlwPM.xkzymc= this.xkzymcTextBox.Text.Trim();
        //分类号
        xwlwPM.flh = this.flhTextBox.Text.Trim();

        //学位授予单位
        xwlwPM.xwsydw = this.xwsydwTextBox.Text.Trim();
        //论文提交日期
        xwlwPM.lwtjrq = DateTime.Parse(this.lwtjrqTextBox.Text);
        //论文答辩日期
        xwlwPM.lwdbrq = DateTime.Parse(this.lwdbrqTextBox.Text);
        //学位授予日期
        xwlwPM.xwsyrq = DateTime.Parse(this.xwsyrqTextBox.Text);
        //中文主题词
        xwlwPM.zwztc = this.zwztcTextBox.Text.Trim();
        //中文摘要
        xwlwPM.zwzy = this.zwzyTextBox.Text.Trim();
        //英文主题词
        xwlwPM.ywztc = this.ywztcTextBox.Text.Trim();
        //英文摘要
        xwlwPM.ywzy = this.ywzyTextBox.Text.Trim();
        //年份
        xwlwPM.nf = Int32.Parse(this.nfTextBox.Text);
        //出版日期
        xwlwPM.cbrq = DateTime.Parse(this.cbrqTextBox.Text);
        //页数
        xwlwPM.ys = Int32.Parse(this.ysHiddenField.Value);
        //更新日期
        xwlwPM.gxrq = DateTime.Now;
        //全文
        xwlwPM.qw = "1";
        //登录人
        xwlwPM.dlr = this.dlrTextBox.Text;

        return xwlwPM;

    }

    /// <summary>
    /// 获取下一个文件名，包括后缀
    /// </summary>
    /// <param name="wjm"></param>
    /// <returns></returns>
    private String GetWJM(String wjm)
    {
        String result = null;
        if (null == wjm || "".Equals(wjm))
        {
            result = this.sybmTextBox.Text + "-00000.PDF";
        }
        else
        {
            //获取文件名的后三位流水号，并在其基础上加1
            int endIndex = wjm.LastIndexOf(".");

            String phString = wjm.Substring(endIndex - 5, 5);

            int ph = 0;

            try
            {
                ph = Int32.Parse(phString);
            }
            catch (Exception)
            {
            }

            //当前篇号+1
            ph += 1;

            phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "0000" + phString;
            }
            else if (phString.Length == 2)
            {
                phString = "000" + phString;
            }
            else if (phString.Length == 3)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 4)
            {
                phString = "0" + phString;
            }
            else if (phString.Length >4)
            {
                String message = "错误：篇名数量已经超过了1000篇";
                Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
                return null;
            }

            result = this.sybmTextBox.Text + "-" + phString+".PDF";
        }
        return result;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.ValidateZWTM_AND_YWTM())
        {
            return;
        }
        if (!this.ValidateWJM())
        {
            return;
        }
        if (!this.ValidateLWTJRQ())
        {
            return;
        }
        if (!this.ValidateLWDBRQ())
        {
            return;
        }
        if (!this.ValidateXWSYRQ())
        {
            return;
        }
        if (!this.ValidateCBRQ())
        {
            return;
        }
        if (!ValidateYWJMFileUpload())
        {
            return;
        }
        XWLW_PM xwlwPM = this.GetXWLW_PM();
        if (OperationFile.Upload(this.ywjmFileUpload, xwlwPM.wjm))
        {
            if (this.xwlwDAO.InsertXWLW_PM(xwlwPM))
            {
                Response.Write("<Script>alert('添加学位论文篇名信息成功')</Script>");
                this.BindData();
            }
        }
        else
        {
            Response.Write("<Script>alert('添加学位论文篇名信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证文件名
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateWJM()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        XWLW_PM xwlwPM = this.xwlwDAO.GetXWLW_PMByWjm(this.wjmTextBox.Text);
        if (null != xwlwPM)
        {
            this.errorLabel.Text = "错误：该文件名已存在";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证论文提交日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateLWTJRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.lwdbrqTextBox.Text = this.lwdbrqTextBox.Text.Trim();
        try
        {
            DateTime.Parse(this.lwdbrqTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：论文提交日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证论文答辩日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateLWDBRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.lwdbrqTextBox.Text = this.lwdbrqTextBox.Text.Trim();

        try
        {
            DateTime.Parse(this.lwdbrqTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：论文答辩日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证出版日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateCBRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.cbrqTextBox.Text = this.cbrqTextBox.Text.Trim();

        try
        {
            DateTime.Parse(this.cbrqTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：出版日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证学位授予日期
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateXWSYRQ()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        this.xwsyrqTextBox.Text = this.xwsyrqTextBox.Text.Trim();
        try
        {
            DateTime.Parse(this.xwsyrqTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：学位授予日期不正确";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证中英文题名
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateZWTM_AND_YWTM()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if((null==this.zwtmTextBox.Text||"".Equals(this.zwtmTextBox.Text.Trim()))&&(null==this.ywtmTextBox.Text||"".Equals(this.ywtmTextBox.Text.Trim())))
        {
            this.errorLabel.Text = "错误：中文题名和英文题名至少有一个不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证上传文件
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateYWJMFileUpload()
    {
        this.errorLabel.Text = "";
        Boolean flag = true;
        if (null == this.ywjmFileUpload || !this.ywjmFileUpload.HasFile)
        {
            this.errorLabel.Text = "错误：请选择上传的文件";
            flag = false;
        }
        else
        {
            if (!this.ywjmFileUpload.FileName.EndsWith(".PDF", true, null))
            {
                this.errorLabel.Text = "错误：请选择PDF类型文件";
                flag = false;
            }
            else
            {
                PdfReader pdf = new PdfReader(this.ywjmFileUpload.FileBytes);
                if (null != pdf)
                {
                    //文件大小不能超过10M
                    if (pdf.FileLength >= 10 * 1024 * 1024)
                    {
                        this.errorLabel.Text = "错误：选择的文件大小超过了10M";
                        pdf.Close();
                        return false;
                    }

                    this.ysHiddenField.Value = pdf.NumberOfPages.ToString();
                }

                pdf.Close();
            }
        }
        return flag;
    }
    protected void zzTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zzTextBox.Text = GlobalMethod.Semicolon(this.zzTextBox.Text.Trim());
    }
    protected void dsTextBox_TextChanged(object sender, EventArgs e)
    {
        this.dsTextBox.Text = GlobalMethod.Semicolon(this.dsTextBox.Text.Trim());
    }
    protected void zwztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zwztcTextBox.Text = GlobalMethod.Semicolon(this.zwztcTextBox.Text.Trim());
    }
    protected void ywztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.ywztcTextBox.Text = GlobalMethod.Semicolon(this.ywztcTextBox.Text.Trim());
    }
}